<?php
namespace App\Models\PrizeRecord;

use App\Models\PrizeRecord\PrizeRecord;
use App\Contracts\Repositories\PrizeRecordRepository as PrizeRecordRepositoryContract;
use Illuminate\Support\Facades\Log;

class PrizeRecordRepository implements PrizeRecordRepositoryContract
{
    /**
     * @param $prizeRecord
     * @return mixed
     */
    public function create($data)
    {
        $prizeRecord = new PrizeRecord($data);
        return $prizeRecord->save();
    }

    /**
     * @param $prizeRecord
     * @return bool
     */
    public function modify($prizeRecord)
    {
        $recordId = array_column($prizeRecord, 'id');
        DB::beginTransaction();
        try {
            PrizeRecord::whereIn('id', $recordId)->delete();
            PrizeRecord::insert($prizeRecord);
            DB::commit();
            return true;
        } catch (\Exception $e) {
            Log::info($e->getMessage());
            DB::rollback();
            return false;
        }
    }

    public function update($params,$conditions)
    {
        return PrizeRecord::where($conditions)->update($params);
    }


    public function getByConditions($conditions)
    {
        return PrizeRecord::where($conditions)->get();
    }

    public function getLastOne($conditions)
    {
        // TODO: Implement getLastOne() method.

        return PrizeRecord::query()->where($conditions)->orderBy('updated_at', 'desc')->get()->first();
    }

    public function deleteByConditions($conditions)
    {
        // TODO: Implement deleteByConditions() method.

        return PrizeRecord::query()->where($conditions)->delete();
    }

    public function findListByConditions($conditions)
    {
        // TODO: Implement findListByConditions() method.
        return PrizeRecord::query()->where($conditions)->get();
    }
}
